
#include "tls.h"


namespace	common
{
	tls::tls()
	{
		tls_idx_ = TLS_OUT_OF_INDEXES;
	}

	tls::~tls()
	{
		destroy();
	}

	bool	tls::create()
	{
		assert( tls_idx_ == TLS_OUT_OF_INDEXES );
		if( tls_idx_ != TLS_OUT_OF_INDEXES )
		{
			return false;
		}

		tls_idx_ = ::TlsAlloc();
		if( tls_idx_ == TLS_OUT_OF_INDEXES )
		{
			return false;
		}

		return true;
	}

	void	tls::destroy()
	{
		if( tls_idx_ != TLS_OUT_OF_INDEXES )
		{
#ifdef	_DEBUG
			BOOL result =
#endif
			::TlsFree( tls_idx_ );
#ifdef	_DEBUG
			assert( result != FALSE );
#endif
			tls_idx_ = TLS_OUT_OF_INDEXES;
		}
	}

	void*	tls::get()	const
	{
		assert( tls_idx_ != TLS_OUT_OF_INDEXES );

		void* p = NULL;
		if( tls_idx_ != TLS_OUT_OF_INDEXES )
		{
			p = ::TlsGetValue( tls_idx_ );
		}

		return p;
	}

	bool	tls::set( void* p )
	{
		assert( tls_idx_ != TLS_OUT_OF_INDEXES );
		if( tls_idx_ != TLS_OUT_OF_INDEXES )
		{
			if( ::TlsSetValue( tls_idx_, p ) == FALSE )
			{
				return false;
			}

			return true;
		}

		return false;
	}
}
